<template>
	<view class="">
		<view class="category-page">
		  <!-- 左侧分类菜单 -->
		  <scroll-view scroll-y class="category-menu" :scroll-top="menuScrollTop">
		    <view 
		      v-for="(item, index) in categories" 
		      :key="index"
		      :class="['menu-item', activeCategoryId === index ? 'active' : '']"
		      @click="changeCategory(index)"
		    >
		      {{ item }}
		    </view>
		  </scroll-view>
		
		  <!-- 右侧文章列表 -->
		  <scroll-view 
		    scroll-y 
		    class="article-list" 
		    @scrolltolower="loadMore"
		    :scroll-with-animation="true"
		  >
		   <!-- 列表渲染 -->
		   <uni-list-item :to="'/pages/photo/detail/detail?id=' + item.id +'&isRandom=true'"  v-for="(item, index) in articles"
		   	:key="index">
		   	<!-- 通过header插槽定义列表左侧图片 -->
		   	<template v-slot:header>
		   		<image class="thumbnail" :src="item.coverUrl" mode="aspectFill" v-show="item.coverUrl"></image>
		   	</template>
		   	<!-- 通过body插槽定义布局 -->
		   	<template v-slot:body>
		   		<view class="main" style="min-height: 70px;">
		   			<text class="title">{{ item.title }}</text>
		   			<text class="description">{{ item.description }}</text>
		   			<view class="info">
		   				<span class="author">{{ item.authorName ? item.authorName : '' }}</span>
		   				<span class="publish_date">{{ publishTime(item.createTime) }}</span>
		   			</view>
		   		</view>
		   	</template>
		   </uni-list-item>
		    <view v-if="loading" class="loading-text">加载中...</view>
		    <view v-if="noMore" class="no-more">没有更多了~</view>
		  </scroll-view>
		</view>
	</view>
</template>

<script>
	import translatePublishTime from "@/pages/photo/common/publish-time";
	import request from '../../../utils/request.js';
export default {
	
  data() {
    return {
      categories: ['美食','旅行','科技','游戏','动漫','体育'],       // 分类数据
      activeCategoryId: 0,  // 当前选中分类ID
      articles: [],         // 文章列表数据
      page: 1,              // 当前页码
      pageSize: 10,         // 每页条数
      total: 0,             // 总条数
      loading: false,       // 加载状态
      noMore: false,        // 是否没有更多
      menuScrollTop: 0,      // 菜单滚动位置
	  navBarHeight: 0,
    }
  },
  onLoad() {
	  this.navBarHeight = uni.getSystemInfoSync().statusBarHeight
      this.getArticles(0)
  },
  methods: {
    // 获取分类文章列表
    getArticles(id) {
		request({
			url: '/articles/getListByCategoryType',
			data: {
				id: id
			}
		}).then(res => {
			this.articles = res.data;
			console.log(res)
		})
	},
	//
	changeCategory(index) {
		this.activeCategoryId = index 
		this.getArticles(index)
	},
    // 加载更多
    loadMore() {
      if (!this.noMore) {
        this.loadArticles()
      }
    },
    // 格式化时间戳
    publishTime(timestamp) {
    	return translatePublishTime(timestamp)
    },
    // 跳转详情
    navigateToDetail(articleId) {
      uni.navigateTo({
        url: `/pages/article/detail?id=${articleId}`
      })
    }
  }
}
</script>

<style scoped>
.navBar {
	width: 100%;
}
	
.category-page {
  display: flex;
  height: 100vh;
}

.category-menu {
  width: 180rpx;
  height: 100%;
  background-color: #f8f8f8;
}

.menu-item {
  padding: 30rpx 20rpx;
  text-align: center;
  font-size: 28rpx;
  color: #666;
  border-left: 6rpx solid transparent;
}

.menu-item.active {
  color: #007aff;
  background-color: #fff;
  border-left-color: #007aff;
  font-weight: bold;
}

.article-list {
  flex: 1;
  padding: 20rpx;
  height: 100%;
}

.loading-text, .no-more {
  text-align: center;
  padding: 20rpx;
  color: #999;
  font-size: 24rpx;
}


.thumbnail {
		width: 240rpx;
		height: 160rpx;
		margin-right: 10rpx;
	}

	.main {
		justify-content: space-between;
		flex: 1;
	}

	.title {
		font-size: 32rpx;
		color: black;
		display: -webkit-box;
		
		-webkit-box-orient: vertical;
		
		-webkit-line-clamp: 1;
		
		overflow: hidden;
	}
	
	.description {
		font-size: 26rpx;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}

	.info {
		width: 100%;
		display: flex;
		font-size: 28rpx;
		color: #999999;	
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

    .author {
		width: 50%;
	}
	
	.author,
	.publish_date {
		-webkit-line-clamp: 1;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		overflow: hidden;
		
	}
	

	.nav-box {
		background-color: #FFFFFF;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;

		/* #ifndef APP-PLUS */
		z-index: 9;
		/* #endif */
	}

	.nav {
		display: flex;
		align-items: center;
		flex-direction: row;
	}

	.uni-search-box {
		flex: 1;
		padding: 0 10px;
	}

	.uni-search-box ::v-deep .uni-searchbar {
		padding: 0;
	}

	.uni-search-box ::v-deep .uni-searchbar__box {
		height: 32px;
	}
</style>